package com.kuang.gulimall.product.controller;

import com.kuang.common.utils.PageUtils;
import com.kuang.common.utils.R;
import com.kuang.gulimall.product.entity.AttrEntity;
import com.kuang.gulimall.product.entity.AttrGroupEntity;
import com.kuang.gulimall.product.service.AttrAttrgroupRelationService;
import com.kuang.gulimall.product.service.AttrGroupService;
import com.kuang.gulimall.product.service.AttrService;
import com.kuang.gulimall.product.service.CategoryService;
import com.kuang.gulimall.product.vo.AttrGroupRealtionVo;
import com.kuang.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 属性分组
 *
 * @author xiangmou
 * @email 1140968804@qq.com
 * @date 2021-05-05 18:18:48
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    ///product/attrgroup/{catelogId}/withattr
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId){
        //1，查出当前分类下的所有属性分组
        //2，查出每个属性分组的所有属性

        List<AttrGroupWithAttrsVo> vos =  attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);

        return R.ok().put("data",vos);

    }

    @PostMapping("/attr/relation")
    public R addRealtion(@RequestBody List<AttrGroupRealtionVo> vos){
        attrAttrgroupRelationService.addRelation(vos);
        return R.ok();
    }


    @GetMapping("/{attrgroupId}/noattr/relation")
    public R getgroupsingle(@PathVariable("attrgroupId") Long attrgroupId,
                            @RequestParam Map<String, Object> params){
        PageUtils page = attrService.getNoRealtion(attrgroupId,params);
        return R.ok().put("page",page);
    }

    //这里的请求参数是一个数组，用数组接受
    @PostMapping("/attr/relation/delete")
    ///product/attrgroup/attr/relation/delete
    public R deleteRelation(AttrGroupRealtionVo[] vos){
        attrService.deleteRelation(vos);
        return R.ok();
    }

    //查询属性分组,做关联
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId){
        //获取当前分组关联的所有属性
        List<AttrEntity> data =  attrService.getRelationAttr(attrgroupId);

        return R.ok().put("data",data);
    }


    /**
     * 列表
     */
    //这里@PathVariable是路径变量
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catelogId") Long catelogId) {
//        PageUtils page = attrGroupService.queryPage(params);

        PageUtils page = attrGroupService.queryPage1(params,catelogId);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {

        //这里不能查空，查空就无效了
        //查询属性分组
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        Long catelogId = attrGroup.getCatelogId();

        Long[] path = categoryService.findCatelogPath(catelogId);
        attrGroup.setCatelogPath(path);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
